<?php
class HomeController extends Zend_Controller_Action
{
	protected $db; // 数据库指针

	// 初始化
	function init()
	{
		// 取得数据库指针
		$this->db=Zend_Registry::get('database');
		$this->view->action=$this->_getParam('action');
		$this->view->controller=$this->_getParam('controller');

		// 页面css和js
		$this->view->javascript=array('paper.js');
	}

	// 处理no action异常
	function __call($method, $args)
	{
		if ('Action'==substr($method, -6)) {
			$this->view->action=substr($method, 0, -6);
			$this->view->param=$this->_getAllParams();
			$this->renderScript('error/no-action.php');
		}
		else // 其他异常
			$this->_redirect('/');
	}

	/* 下面是action处理函数: ***************************/

	// 首页
	function indexAction()
	{
		$this->view->title='吉林大学软件教研室';
		$paper=new paper(); // paper module db
		$this->view->paper=$paper->find(0, '`Date` desc', 20);
		$info = new info; // info module db
		$this->view->news = info::format($info->fetch(false), 0, 2);
	}

	// 实验室介绍
	function introAction()
	{
		$this->view->title='介绍－软件教研室';
	}

	// 研究方向
	function interestAction()
	{
		$this->view->title='研究方向－软件教研室';
	}

	// 研究成果
	function fruitAction()
	{
		$this->view->title='研究成果－软件教研室';
	}

	// 后台管理首页
	function adminAction()
	{
		$aid = auth::aid();
		if (empty($aid)) {
			$this->view->msg = '你的权限不够，请先登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}

		$this->view->title = '后台管理－软件教研室';
	}

	// 项目
	function projectAction()
	{
		$this->view->title = '教研室项目－软件教研室';
		$this->view->html = html::Load('project');
		$this->view->name = 'project';
		$this->renderScript('home/static.php');
	}

	// 书籍
	function bookAction()
	{
		$this->view->title = '书籍著作－软件教研室';
		$this->view->html = html::Load('book');
		$this->view->name = 'book';
		$this->renderScript('home/static.php');
	}

	// 奖励
	function awardAction()
	{
		$this->view->title = '所获奖励－软件教研室';
		$this->view->html = html::Load('award');
		$this->view->name = 'award';
		$this->renderScript('home/static.php');
	}

	// 修改静态页面
	function editAction()
	{
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg = '你的权限不够，请先登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}

		$name = $this->_getParam('name');
		$this->view->title = '修改－软件教研室';
		$this->view->html = html::Load($name, false);
		$this->view->name = $name;
	}

	// 增加一个静态页面
	function addAction()
	{
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg = '你的权限不够，请先登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}

		$name = $this->_getParam('name');
		$cont = $this->_getParam('content');
		$file = $this->_getParam('file');
		html::Save($name, $file, $cont);
		$url = '/home/'.$name;
		$this->_redirect($url);
	}

	// 删除一个静态页
	function delAction()
	{
		if (!auth::check(auth::EDIT_LAB_STUFF)) {
			$this->view->msg = '你的权限不够，请先登录。';
			$this->renderScript('error/small-msg.php');
			return;
		}

		$name = $this->_getParam('name');
		$file = $this->_getParam('file');
		$result = html::Del($name, $file);
		if ($result)
			echo '1';
		else
			echo '0';
		$this->_helper->viewRenderer->setNoRender();
	}

};
?>
